package com.zut.subject.domain.handler.subject;

import com.zut.subject.common.enums.SubjectTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class SubjectHandlerFactory implements InitializingBean {

    @Resource
    List<SubjectTypeHandler> subjectTypeHandlerList;

    Map<SubjectTypeEnum,SubjectTypeHandler> map =new HashMap<>();

    //通过题目类型id获得该类型处理方法
    public SubjectTypeHandler getHandlerBySubjectTypeId(Integer subjectTypeId){
        SubjectTypeEnum subjectTypeEnum =SubjectTypeEnum.getByCode(subjectTypeId);
        return map.get(subjectTypeEnum);
    }

    //在springboot初始化bean对象时执行该方法
    @Override
    public void afterPropertiesSet() throws Exception {
        //将每一次bean初始化时，将每一个处理方法和其对应的枚举注入map
        for (SubjectTypeHandler subjectTypeHandler : subjectTypeHandlerList) {
            map.put(subjectTypeHandler.subjectEnum(),subjectTypeHandler);

        }
    }
}
